package com.jtdemo.stq.demo.service.impl;

import com.jtdemo.stq.demo.dao.PersonDao;
import com.jtdemo.stq.demo.entity.Person;
import com.jtdemo.stq.demo.service.PersonService;
import com.jtdemo.stq.demo.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/*
 **@Description: TODO
 **@ClassName: PersonServiceImpl
 **@Author: wjt
 **@Time: 2022/6/24 18:57
 **@Version：1.0
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDao personDao;
    @Override
    public ResultVo isLogin(String name,String pwd) {
        List<Person> people = personDao.listPersonByName(1,name);
        if(people.size()>0&&"123456".equals(pwd)){ //没有密码字段，用123456暂时代替
            return new ResultVo(1,"有该用户,可登录",null);
        }else {
            return new ResultVo(0,"不可登录",null);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo addPerson(Person person) {
        if("男".equals(person.getSex())){
            person.setSex("M");
        }else{
            person.setSex("W");
        }
        int res = personDao.addPerson(person);
        if(res>0)
            return new ResultVo(1,"success",null);
        return new ResultVo(0,"fail",null);
    }

    @Override
    public ResultVo showPagePerson(int currentPage) {
        HashMap<String,Object> map=new HashMap<>();

        List<Person> people =personDao.listPersonbyCurrentPage(currentPage);
        if(people.size()==0){
            return new ResultVo(0,"fail",null);
        }
        int count=personDao.countPerson();
        map.put("count",count);
        map.put("pageSize",3);
        map.put("listPerson",people);
        return new ResultVo(1,"success",map);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ResultVo updatePerson(Person person) {
        if("男".equals(person.getSex())){
            person.setSex("M");
        }else{
            person.setSex("W");
        }
        int res = personDao.updatePeopleById(person);
        if(res>0)
            return new ResultVo(1,"success",null);
        return new ResultVo(0,"fail",null);
    }

    @Override
    public ResultVo deletePerson(int id) {
        int res = personDao.deletePeopleById(id);
        if(res>0)
            return new ResultVo(1,"success",null);
        return new ResultVo(0,"fail",null);
    }

    @Override
    public ResultVo selectPersonByName(int page,String name) {
        HashMap<String,Object> map=new HashMap<>();
        List<Person> people = personDao.listPersonByName(page,name);
        if(people.size()==0){
            return new ResultVo(0,"fail",null);
        }
        int count=personDao.countPerson();
        map.put("count",people.size());
        map.put("pageSize",3);
        map.put("listPerson",people);
        return new ResultVo(1,"success",map);
    }
}
